﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using IEstate.Web.Models;
using IEstate.Core.Models;
using IEstate.Repositories.Database;

namespace IEstate.Web.Controllers
{
    [Authorize(Users = "anastasia, admin")]
    public class AdminController : Controller
    {
        private EstateContext DBContext = new EstateContext();
        private ApplicationDbContext DB = new ApplicationDbContext();

        //
        // GET: /Admin/
        public ActionResult Index()
        {
            IEnumerable<Estate> query;
            query = DBContext.Estates.Select(p => p);

            return View("Index", query);
        }

        [HttpGet]
        public ActionResult Approve()
        {
            int query = DBContext.Approved.Count();
            if (query == 0)
                ViewBag.number = 0;
            else
                ViewBag.number = query;

            var list = DBContext.Approved;
            ViewBag.counter = 0;

            return View("Approve", list);
        }

        [HttpGet]
        public ActionResult Accept(int id = 1)
        {
            Approved approvedEstate = DBContext.Approved.FirstOrDefault(p => p.ApprovedID == id);
            
            Estate estate = new Estate
            {
                Street = approvedEstate.Street,
                Bedrooms = approvedEstate.Bedrooms,
                Size = approvedEstate.Size,
                Price = approvedEstate.Price,
                UID = approvedEstate.UID,
                Date = DateTime.Now,
                City = approvedEstate.City
            };

            DBContext.Approved.Remove(approvedEstate);
            DBContext.Estates.Add(estate);
            DBContext.SaveChanges();

            return RedirectToAction("Approve");
        }

        [HttpGet]
        public ActionResult DisplayUsers()
        {
            IEnumerable<ApplicationUser> query;
            query = DB.Users.Select(p => p);
            return View("DisplayUsers", query);
        }

        [HttpGet]
        [AllowAnonymous]
        public ActionResult Promo()
        {
            IEnumerable<Estate> query;
            query = DBContext.Estates.Where(p => p.Promotional == true);

            return View("Promo", query);
        }

        [HttpGet]
        public ActionResult Edit(int id = 1)
        {
            return View("Edit", new EstateViewModel());
        }

        [HttpPost]
        public ActionResult Edit(int id, EstateViewModel estateViewModel, HttpPostedFileBase image)
        {
            // Skip model validation because in case of only one field update
            // User must supply every field a value

            Estate query;
            query = DBContext.Estates.FirstOrDefault(p => p.EstateID == id);

            if (estateViewModel.Bedrooms != null) query.Bedrooms = (int)estateViewModel.Bedrooms;
            if (estateViewModel.Price != null) query.Price = (int)estateViewModel.Price;
            if (estateViewModel.Size != null) query.Size = (int)estateViewModel.Size;
            if (estateViewModel.Promotional == true) query.Promotional = true;
            if (estateViewModel.Info != null) query.Info = estateViewModel.Info;

            EImage Image = new EImage();
            
            if(image != null)
            {
                Image.EstateID = id;
                Image.ImageMimeType = image.ContentType;
                Image.ImageData = new byte[image.ContentLength];
                image.InputStream.Read(Image.ImageData, 0, image.ContentLength);

                DBContext.EImages.Add(Image);
            }

            DBContext.SaveChanges();

            ViewBag.Id = id;

            return View("_Edit");
        }

        [HttpGet]
        public ActionResult Delete(int id = 1)
        {
            Estate query;
            query = DBContext.Estates.Where(p => p.EstateID == id).FirstOrDefault();

            DBContext.Estates.Remove(query);
            DBContext.SaveChanges();

            ViewBag.Id = id;

            return View("Delete");
        }
	}
}